package com.orhon.smartcampus.modules.systemctl.fetcher;

import java.util.ArrayList;
import java.util.List;

import com.orhon.smartcampus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.systemctl.dao.OperationDepartmentRelationDao;
import com.orhon.smartcampus.modules.systemctl.entity.OperationDepartmentRelation;
import com.orhon.smartcampus.modules.systemctl.entity.Operations;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDuties;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDepartment;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDuty;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLOperation;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLOperationDepartmentRelation;
import com.orhon.smartcampus.modules.systemctl.mm.DepartmentMapper;
import com.orhon.smartcampus.modules.systemctl.mm.DutiesMapper;
import com.orhon.smartcampus.modules.systemctl.mm.OperationDepartmentRelationMapper;
import com.orhon.smartcampus.modules.systemctl.mm.OperationMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class OperAndDeparAndDutyFetcher implements DataFetcher {


	@Autowired
	OperationDepartmentRelationDao OperationDepartmentRelationDao;

	@Autowired
	TokenService tokenService;

	/**
	 * 获取部门列表
	 *
	 * @param environment
	 * @return
	 * @throws Exception editor : ths
	 */
	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		List<GQLOperationDepartmentRelation> departments = new ArrayList<>();

		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		int id = Integer.parseInt(env.getArgument("id").toString());
		String hql = "from OperationDepartmentRelation where deleted_at = null and department_id = " + id+" and school_id = " + schoolId;


		List<OperationDepartmentRelation> list = OperationDepartmentRelationDao.find(hql, null, 0,0);


		for (OperationDepartmentRelation item : list) {
			GQLOperation gqloperations = null;
			if (env.getSelectionSet().contains("operation_id")) {
				Operations operations = item.getSystemctlOperationsByOperationId();
				gqloperations = OperationMapper.MAPPER.toOperations(operations);
			}
			GQLDuty gqlorgDuties = null;
			if (env.getSelectionSet().contains("duty_id")) {
				OrgDuties duties = item.getSystemctlOrgDutiesByParentId();
				gqlorgDuties = DutiesMapper.MAPPER.toOrgDuties(duties);
			}
			GQLDepartment gqlDepartment = null;
			if (env.getSelectionSet().contains("department_id")) {
				OrgDepartments orgDepartments = item.getSystemctlOrgDepartmentsByDepartmentId();
				gqlDepartment = DepartmentMapper.MAPPER.toOrgDepartments(orgDepartments);
			}
			
			GQLOperationDepartmentRelation relation = OperationDepartmentRelationMapper.MAPPER.toOperationDepartmentRelation(item);
			relation.setDuty_id(gqlorgDuties);
			relation.setOperation_id(gqloperations);
			relation.setDepartment_id(gqlDepartment);
			departments.add(relation);
		}

		return departments;
	}
}
